package com.xju.subjectsystem.controller;

import com.xju.subjectsystem.pojo.Books;
import com.xju.subjectsystem.pojo.Result;
import com.xju.subjectsystem.service.BooksService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: xju_-course_-kg_-master
 * @package: com.xju.subjectsystem.controller
 * @className: BooksController
 * @author: Eiard
 * @description: TODO : 
 * @date: 2024-12-17 02:07
 */
@RestController
@RequestMapping("/books")
public class BooksController {

    private final BooksService booksService;

    public BooksController(BooksService booksService) {
        this.booksService = booksService;
    }


    /**
     * @methodName: selectByCsno
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 查询班级对应的教材
     * @author: Eiard
     * @date: 2024-12-17 02:08
     * @param: csno
     * @return: Result
     */
    @GetMapping("/selectByCsno")
    public Result selectByCsno(Integer csno) {

        System.out.println(csno);

        List<Books> booksList = booksService.selectByCsno(csno);

        if (booksList == null || booksList.isEmpty()) {
            return Result.error("无对应教材");
        } else {
            return Result.success(booksList);
        }
    }

    /**
     * @methodName: deleteByBno
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 删除班级对应教材或者参考书
     * @author: Eiard
     * @date: 2024-12-17 02:17
     * @param: bno
     * @return: Result
     */
    @DeleteMapping("/deleteByBno")
    public Result deleteByBno(Integer bno) {
        try {
            booksService.deleteByBno(bno);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }

    /**
     * @methodName: add
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 批量为课程班级添加教材或参考书
     * @author: Eiard
     * @date: 2024-12-17 02:21
     * @param: booksList
     * @return: Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody List<Books> booksList) {
        Map<String, List<String>> resultData = booksService.batchAddBooks(booksList);

        List<String> successList = resultData.get("successList");
        List<String> failList = resultData.get("failList");


        if (successList.isEmpty()) {
            return Result.error("全部导入失败", resultData);
        } else if (!failList.isEmpty()) {
            return Result.success("部分导入成功", resultData);
        } else {
            return Result.success("全部导入成功", resultData);
        }

    }

    /**
     * @methodName: update
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 修改课程对应教材的名称和类型
     * @author: Eiard
     * @date: 2024-12-17 02:22
     * @param: books
     * @return: Result
     */
    @PutMapping("/update")
    public Result update(@RequestBody Books books) {
        try {
            booksService.updateBooks(books);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }

}
